import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:maihealth/class/login/model/userModel.dart';
import 'package:maihealth/class/login/provide/loginProvide.dart';
import 'package:maihealth/class/main/function.dart';
import 'package:maihealth/class/main/mainPage.dart';
import 'package:maihealth/class/main/myAppBar.dart';
import 'package:maihealth/utils/constant.dart';
import 'package:maihealth/utils/hud.dart';
import 'package:maihealth/utils/router.dart';
import 'package:maihealth/utils/screenUtil.dart';
import 'package:maihealth/utils/toast.dart';
import 'package:provide/provide.dart';
class loginPwdPage extends StatefulWidget {
  final String tel;
  const loginPwdPage({Key key, this.tel}) : super(key: key);
  @override
  _loginPwdPageState createState() => _loginPwdPageState();
}


class _loginPwdPageState extends State<loginPwdPage> {
TextEditingController editingController = TextEditingController();
FocusNode _focusNode = FocusNode();
bool isFocus = false;
bool isEnable = false;
BuildContext hudctx;
 @override
  void initState() {
   _focusNode.addListener((){
      setState(() {
        isFocus =  _focusNode.hasFocus;
      });
    });
    //监听文本变化
    editingController.addListener((){
      setState(() {
          isEnable = editingController.text.length>=6;
      });
    });
     super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return ProgressHUD(
        child:Builder(
          builder:(ctx){
          hudctx = ctx;
          return  Scaffold(
              appBar:myAppBar(),
              body:  GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap:(){//点击失去焦点
                      _exitKeyBoard();
                  },
                  child: Container(
                  padding: EdgeInsets.fromLTRB(setWidth(50), setHeight(40), setWidth(50), 0),
                  color: Colors.white,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        _topTitle(),
                        _loginTextField(),
                        _line(),
                        _loginBtn()
                      ],
                  )
                ),
             )
           );
        }));
  }

  
  Widget _topTitle(){
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text('您的手机号',style:TextStyle(fontSize: setFont(20),fontWeight: FontWeight.bold)),
        SizedBox(height: setHeight(5)),
        Text(widget.tel ,style:TextStyle(fontSize: setFont(17))),
      ],
    );
  }

    //文本输入框
  Widget _loginTextField() {
    return Container(
      height: setHeight(80),
      padding: EdgeInsets.only(top: setHeight(35)),
      child: CupertinoTextField(
        focusNode: _focusNode,
        controller: editingController,
        keyboardType: TextInputType.number,
        clearButtonMode: OverlayVisibilityMode.editing,
        placeholder: '请输入登录密码',
        obscureText: true,
        style: TextStyle(fontSize: setFont(20)),
        decoration: BoxDecoration(
            border: null
        ),
      ),
     ) ;
    }

      //分割线
  Widget _line(){
    return  Divider(color: isFocus ? ThemeColor : ThemeLineColor);
  }


    //登录按钮
  Widget _loginBtn(){
    return Container(
        height: setHeight(48),
        width: setWidth(275),
        margin: EdgeInsets.only(top: setHeight(10)),
        child: RaisedButton(
          onPressed: !isEnable ? null : (){
            _exitKeyBoard();
            _requsetLogin();
          },
          child: Text('登录',style: TextStyle(color: Colors.white),),
          disabledColor: ThemeDisableColor,
          color: ThemeColor,
          shape: StadiumBorder(),
        ),
      );
     }

 //退出键盘
 void _exitKeyBoard(){
      //一个小技巧,让他聚焦到下一个不存在的textField,这样键盘就会退出
      FocusScope.of(context).requestFocus(FocusNode());
 }

///请求登录
 void _requsetLogin() async{
      showHud(hudctx);
      final provide = Provide.value<loginProvide>(context);
      var pwd = editingController.text;

      try {
            userModel model = await provide.requestLogin(widget.tel,md5String(pwd));
            print('uid==${model.nickname}');
            dismissHud(hudctx);
            Router.changeRootPage(context,mainPage());
      } catch (e) {
            showToast(e);
            dismissHud(hudctx);
      }
 }
}
